package com.ykdl.member.kid.person;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.http.Network;
import com.ykdl.member.kid.adapters.AttenAdapter;
import com.ykdl.member.kid.beans.Memberlistbean;
import com.ykdl.member.kid.beans.StatuResult;
import com.ykdl.member.kid.beans.UserActorBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class AttentionActivity extends BaseScreen implements OnClickListener, OnItemClickListener{
	
	public static final String TAG = "AttentionActivity";
	
	private RefreshListView fans_list;
	private AttenAdapter mAdapter;
	private TextView main_title, fans_num;
	//private View foot = null;
	private LinearLayout more_fans_ll;
	
	private int start_id = 0;
	private int cursor = 0;
	private int count = 50;
	
	private int actor_id;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_attention);
		init();
		actor_id = getIntent().getIntExtra("actor_id", 0);
		getAttentionList(actor_id, start_id, cursor, count, true);
	}
	
	@Override
	protected void onResume() {
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}

	public void init() {
		//foot = LayoutInflater.from(mContext).inflate(R.layout.fans_item_foot, null);
		//more_fans_ll = (LinearLayout) foot.findViewById(R.id.more_fans_ll);
		//foot.findViewById(R.id.more_fans).setOnClickListener(this);
		findViewById(R.id.btnBack).setOnClickListener(this);
		main_title = (TextView) findViewById(R.id.main_title);
		fans_num = (TextView) findViewById(R.id.fans_num);
		mAdapter = new AttenAdapter(this);
		fans_list = (RefreshListView) findViewById(R.id.fans_list);
		//fans_list.addFooterView(foot);
		fans_list.setOnItemClickListener(this);
		fans_list.setMoreButtoIsGon(true);
		fans_list.setAdapter(mAdapter);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
//		case R.id.more_fans:
//			main_title.setText("粉丝");
//			fans_num.setVisibility(View.GONE);
//			more_fans_ll.setVisibility(View.GONE);
//			break;
		case R.id.iask_root_loadmore:
			//Network.getInstance().requestBodyRecordList(cursor, count, type, mBodyListTag);
			getAttentionList(actor_id, start_id, cursor, count, false);
			break;
		default:
			break;
		}
	}

	public void getAttentionList(int actor_id, int start_id, int cursor, int count, boolean show) {
		if (show) showProgress("正在发送请稍等...");
		String pageurl;
		if (actor_id != 0) {
			pageurl = KidConfig.BASE_SERVER+"/api/v1/friendship/"+String.valueOf(actor_id)+"/friends";
		} else {
			pageurl = KidConfig.GET_ATTENTION_LIST;
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("start_id", String.valueOf(start_id));
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(pageurl, map, null);
		TaskManager.startHttpRequest(request, new GetFasnListTag(), Memberlistbean.class);
	}

	class GetFasnListTag implements ITag<Memberlistbean> {

		@Override
		public void refreshUI(Memberlistbean t, DataState ds) {
			finishProgress();
			if (t != null && t.getList() != null) {
				hidErrorTip( );
				List<UserActorBean> beans = t.getList();
				mAdapter.addUser(beans);
				mAdapter.notifyDataSetChanged();
				if (t.getNext_cursor() == t.getTotal_number()) {
					fans_list.setMoreButtoIsGon(true);
				} else {
					cursor = t.getNext_cursor();
				}
				fans_num.setText(String.valueOf(t.getTotal_number())+"个");
			} else {
				fans_num.setVisibility(View.GONE);
				showErrorTip("没有数据...", "");
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			finishProgress();
			if (TextUtils.isEmpty(parseError.getErrMsg())) {
				Toast.makeText(AttentionActivity.this, "加载数据失败", 1).show();
				return true;
			} else {
				return false;
			}
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		UserActorBean user = (UserActorBean) mAdapter.getItem(position - 1);
		Intent intent = new Intent(this, PersonCenterActivity.class);
		intent.putExtra(PersonCenterActivity.STR_ACTOR_ID, user.getFriend_actor().getActor_id());
		startActivity(intent);
	}

	
}
